<?php

class Core_Model_Article_Collection_Category extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('article/category');
        $this->from('{t}article_category', 'a')
                ->select()->field('a.*');
    }

    public function joinAccount(){
        $this->join()->inner('{t}permission_account', 'c')->on('a.creator_id', 'c.id')
        	 ->closest()
             ->select()->field('c.full_name', 'creator');
        return $this;
    }

    public function joinTemplate(){
         $this->join()
                ->left('{t}design_page_template','d')
                ->on('a.theme_id','d.id')
                ->closest()
                ->select()
                ->field('d.name','theme_name');
        return $this;
    }

    public function toTreeArray(){
        $result = array();
        $this->buildTree($result, 0, 1);
        return $result;
    }

    private function buildTree(&$arr, $parentId, $level){
        for($i = 0; $i < count($this); $i++){
            $item = $this[$i];
            if($item->getParentId() == $parentId){
                $arr[] = $item->getAllData();
                $this->buildTree($arr, $item->getId(), $level + 1);
            }
        }
    }
}